package com.hushao.lims.weblims.service.impl;

import com.hushao.lims.weblims.domain.ConsumableInfo;
import com.hushao.lims.weblims.repository.ConsumableRepository;
import com.hushao.lims.weblims.service.ConsumableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * @author 930588706
 * @date 18/1/18
 */
@Service
public class ConsumableServiceImpl implements ConsumableService {
    @Autowired
    private ConsumableRepository consumableRepository;


    @Override
    public ConsumableInfo findByConsumableNameAndConsumableSpecificationsAndConsumablePrice(String consumableName, String consumableSpecifications, BigDecimal consumablePrice) throws Exception {
        if(consumableName.isEmpty()){
            throw new Exception("耗材名称为空！");
        }
        if(consumableSpecifications.isEmpty()){
            throw new Exception("耗材规格为空！");
        }
        if(consumablePrice==null){
            throw new Exception("耗材价格为空！");
        }
        return consumableRepository.findByConsumableNameAndConsumableSpecificationsAndConsumablePrice(consumableName,consumableSpecifications,consumablePrice);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ConsumableInfo save(ConsumableInfo consumableInfo) throws Exception {
        return consumableRepository.save(consumableInfo);
    }

    @Override
    public ConsumableInfo findOne(Long consumableId) {
        return consumableRepository.getOne(consumableId);
    }
}
